package com.hehao.java_basic.stream;


import com.hehao.common.entiy.Person;

import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Damon
 * @date 2022/2/23
 * stream collect 操作
 * 归集(toList/toSet/toMap)
 * 统计(count/averaging)
 * 分组(partitioningBy/groupingBy)
 * 接合(joining)
 * 归约(reducing)
 */
public class CollectDemo {
    public static void main(String[] args) {
        List<Person> list = Person.getList();
        /**归集*/
        System.out.println(list.stream().filter(p -> p.getSalary() > 8000).collect(Collectors.toMap(Person::getName, p -> p)));

        /**统计  统计员工人数、平均工资、工资总额、最高工资*/
        System.out.println(list.stream().collect(Collectors.counting()));

        System.out.println(list.stream().collect(Collectors.averagingInt(Person::getSalary)));

        System.out.println(list.stream().collect(Collectors.summingInt(Person::getSalary)));

        IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(Person::getSalary));
        System.out.println(collect);

        /**分组 将员工按薪资是否高于8000分为两部分；将员工按性别和地区分组*/
        Map<Boolean, List<Person>> part = list.stream().collect(Collectors.partitioningBy(p -> p.getSalary() > 8000));
        System.out.println(part);

        System.out.println(list.stream().collect(Collectors.groupingBy(Person::getSex)));

        Map<String, Map<String, List<Person>>> doubleGroup = list.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
        System.out.println(doubleGroup);

        /**接合 */
        System.out.println(list.stream().map(Person::getName).collect(Collectors.joining(",")));

        /**归约 */
        System.out.println(list.stream().collect(Collectors.reducing(0, Person::getSalary, (i, j) -> (i + j - 5000))));
        System.out.println(list.stream().map(Person::getSalary).reduce(0, (i, j) -> (i + j - 5000)));


    }
}
